package com.m3958.logcollector.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationTrustResolver;
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.m3958.logcollector.vo.UserState;

@Controller
public class LoginStateController {

    @Autowired
    private UserStateFactory userStateFactory;
    
    private AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl();

    @RequestMapping(value = "/login/state", method = RequestMethod.GET)
    public ResponseEntity<UserState> loginStatus(HttpServletRequest request, Model model) {
        Authentication an = SecurityContextHolder.getContext().getAuthentication();
        UserState us;
        if (an == null || trustResolver.isAnonymous(an)) {
            us = userStateFactory.getUserState(false);
        } else {
            us = userStateFactory.getUserState(true);
        }
        return ResponseEntity.ok(us);
    }
    
}
